﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("ETL", "GetWaitDataVerifyDataList", "1.0")]
    public class GetWaitDataVerifyDataListAction : BaseAction<GetWaitDataVerifyDataListParams, GetWaitDataVerifyDataListResult>
    {
        protected override ActionResult<GetWaitDataVerifyDataListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetWaitDataVerifyDataListParams opParams)
        {
            SER.DataService service = new SER.DataService();
            ServiceResult<MOD.MDtTableData> serResult = service.GetDataList(opParams.TaskId, opParams.Page * 100, 100);
            if (serResult.IsSuccess)
            {
                GetWaitDataVerifyDataListResult actResult = new GetWaitDataVerifyDataListResult();
                actResult.Page = opParams.Page;
                actResult.Start = serResult.Data.Start;
                actResult.Limit = serResult.Data.Limit;
                actResult.Total = serResult.Data.Total;
                actResult.Count = serResult.Data.DataTable.Rows.Count;

                List<string> columns = new List<string>();
                List<string> types = new List<string>();
                for (int colIndex = 0; colIndex < serResult.Data.DataTable.Columns.Count; colIndex++)
                {
                    columns.Add(serResult.Data.DataTable.Columns[colIndex].ColumnName);
                    types.Add(serResult.Data.DataTable.Columns[colIndex].DataType.FullName);
                }
                actResult.Columns = columns;
                actResult.Types = types;

                for (int rowIndex = 0; rowIndex < serResult.Data.DataTable.Rows.Count; rowIndex++)
                {
                    System.Data.DataRow dataRow = serResult.Data.DataTable.Rows[rowIndex];
                    GetWaitDataVerifyDataListResult.Item item = new GetWaitDataVerifyDataListResult.Item();

                    for (int colIndex = 0; colIndex < actResult.Columns.Count; colIndex++)
                    {
                        item.Add(Convert.ToString(dataRow[actResult.Columns[colIndex]]));
                    }

                    actResult.Items.Add(item);
                }

                return new ActionResult<GetWaitDataVerifyDataListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetWaitDataVerifyDataListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetWaitDataVerifyDataListParams
    {
        [Parameter]
        public Guid TaskId { get; set; }

        [Parameter]
        public int Page { get; set; }
    }

    public class GetWaitDataVerifyDataListResult : PagedResult
    {
        public List<string> Columns { get; set; }
        public List<string> Types { get; set; }
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public List<string> Values { get; set; }

            public Item()
            {
                Values = new List<string>();
            }

            public void Add(string value)
            {
                Values.Add(value);
            }
        }
    }
}
